<?xml version="1.0"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:output method="html"/>
  <xsl:template match="/cruisecontrol//buildresults//project">
    <script>
      function ViewState(blockId)
      {
      var block = document.getElementById(blockId);
      if (block.style.display=='none')
      {
      block.style.display='block';
      }
      else
      {
      block.style.display='none';
      }
      }

      function SwitchAll(how)
      {
      var nodes = document.getElementsByTagName("div");
      for (i = 0; i != nodes.length;i++)
      {
      var block = nodes[i];
      if (block != null)
      {
      if (block.className == 'collapsable' )
      {
      block.style.display=how;
      }
      }
      }
      }

      function ExpandAll()
      {
      SwitchAll('block');
      }

      function CollapseAll()
      {
      SwitchAll('none');
      }
    </script>
    <table class="section-table" cellpadding="2" cellspacing="0" border="0" width="98%">
      <tr>
        <td class="sectionheader" colspan="2">
          Compiled project <xsl:value-of select="@name"/> : <xsl:value-of select="@configuration"/>
        </td>
      </tr>
      <tr>
        <td>
          <div class="collapsable" id="project">
            <xsl:call-template name="produce_message"/>
          </div>
        </td>
      </tr>
    </table>
  </xsl:template>

  <xsl:template name="produce_message" match="message">
    <xsl:variable name="messages" select="message" />
    <xsl:if test="count($messages) > 0">
      <xsl:variable name="error.messages" select="$messages[(contains(text(), 'error ') and not(contains(text(), 'warning ') )) or @level='Error'] | /cruisecontrol//builderror/message | /cruisecontrol//internalerror/message" />
      <xsl:variable name="error.messages.count" select="count($error.messages)" />
      <xsl:variable name="warning.messages" select="$messages[(contains(text(), 'warning ')) or @level='Warning']" />
      <xsl:variable name="warning.messages.count" select="count($warning.messages)" />
      <xsl:variable name="info.messages" select="$messages[(contains(text(), '->')) or @level='Info']" />
      <xsl:variable name="info.messages.count" select="count($info.messages)" />
      <xsl:variable name="total" select="count($error.messages) + count($warning.messages)"/>
      <xsl:if test="$error.messages.count > 0">
        <table class="section-table" cellpadding="2" cellspacing="0" border="0" width="98%">
          <tr>
            <a href="#" onclick="javascript: ViewState('errors');">
              <td class="sectionheader">
                Errors: (<xsl:value-of select="$error.messages.count"/>)
              </td>
            </a>
          </tr>
          <tr>
            <td>
              <div class="collapsable" id="errors">
                <table class="section-table" cellSpacing="0" cellPadding="2" width="98%" border="0">
                  <xsl:apply-templates select="$error.messages"/>
                </table>
              </div>
            </td>
          </tr>
        </table>
      </xsl:if>
      <xsl:if test="$warning.messages.count > 0">
        <table class="section-table" cellpadding="2" cellspacing="0" border="0" width="98%">
          <tr>
            <td class="sectionheader">
              Warnings: (<xsl:value-of select="$warning.messages.count"/>)
            </td>
          </tr>
          <tr>
            <td>
              <div class="collapsable" id="errors">
                <table class="section-table" cellSpacing="0" cellPadding="2" width="98%" border="0">
                  <xsl:apply-templates select="$warning.messages"/>
                </table>
              </div>
            </td>
          </tr>
        </table>
      </xsl:if >
      <xsl:if test="$info.messages.count > 0">
        <table class="section-table" cellpadding="2" cellspacing="0" border="0" width="98%">
          <tr>
            <td class="sectionheader">
              Info:
            </td>
          </tr>
          <tr>
            <td>
              <div class="collapsable" id="errors">
                <table class="section-table" cellSpacing="0" cellPadding="2" width="98%" border="0">
                  <xsl:apply-templates select="$info.messages"/>
                </table>
              </div>
            </td>
          </tr>
        </table>
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <xsl:template match="/cruisecontrol//buildresults//finished">
    <table class="section-table" cellpadding="2" cellspacing="0" border="0" width="98%">
      <tr>
        <td class="sectionheader" colspan="2">
          Complete: <xsl:value-of select="@buildtype"/> succeeded: <xsl:value-of select="@succeeded"/> failed: <xsl:value-of select="@failed"/>skipped: <xsl:value-of select="@skipped"/>
        </td>
      </tr>
    </table>
  </xsl:template>

  <!--important section. Look like duplicate but show all messages text-->
  <xsl:template match="message">
    <pre class="section-data">
      <xsl:value-of select="text()"/>
    </pre>
  </xsl:template>
</xsl:stylesheet>